//
//  WBWelcomeView.swift
//  GitWeibo
//
//  Created by 胡金东 on 2017/6/1.
//  Copyright © 2017年 胡金东. All rights reserved.
//

import UIKit
import SDWebImage

class WBWelcomeView: UIView {
    /// - imageView 下边的约束
    @IBOutlet var BottomImage: NSLayoutConstraint!
    @IBOutlet var ImageView: UIImageView!

    @IBOutlet var Label: UILabel!
    class func welcomeView() -> WBWelcomeView {
        let nib = UINib(nibName: "WBWelcomeView", bundle: nil)
        let v = nib.instantiate(withOwner: nil, options: nil)[0] as! WBWelcomeView
        /// - 设置frame ，默认是600*600
        v.frame = UIScreen.main.bounds
        
        
        return v
    }
    
    override func awakeFromNib() {
        
        guard  let urlString = WBNetWorkSessionManager.NetWorkshared().account.avatar_large,
        let url = URL(string: urlString) else {
            return
        }
                
        ImageView.sd_setImage(with: url, placeholderImage: UIImage(named: "avatar_default_big"))
        ImageView.layer.cornerRadius = 42.5
        ImageView.layer.masksToBounds = true
        
        
        
    }
    
    
    /// - 视图添加到Window上，表示视图已经显示
    override func didMoveToWindow() {
        super.didMoveToWindow()
        /// - 先不显示
        Label.alpha = 0
        /// - 当视图添加到window时，根据父视图的大小来布局，那个时候视图还没有被加载，
        /// - layoutIfNeeded()会直接根据当前的约束直接进行布局
        /// - 先更新一下约束
        self.layoutIfNeeded()
        
        /// - 修改imageView bottom的值
        BottomImage.constant = bounds.size.height - 200
        
        /**
         *  Duration : 动画时间
         *  delay : 动画延迟时间
         *  usingSpringWithDamping : 弹性
         *  initialSpringVelocity : 初始速度
         *  options : 可选项，一些可选的动画效果，
         */
        UIView.animate(withDuration: 1.5,
                       delay: 0,
                       usingSpringWithDamping: 0.5,
                       initialSpringVelocity: 0,
                       options: [],
                       animations: {
                        /// - 更新约束
                        self.layoutIfNeeded()
        }){ (_) in
            UIView.animate(withDuration: 1.0, animations: {
                /// - 显示label
                self.Label.alpha = 1
            }, completion: { (_) in
                
                /// - 显示完回到首页
                self.removeFromSuperview()
                
            })
        }
        
        
    }
}
